<template>
  <div class="container">
    <div class="container-1200">
      <div class="flexWithJusBetween mg-B20">
        <div class="title">企业详情表</div>
        <!-- <div>
          <el-button type="primary" @click="addCompany">添加企业</el-button>
          <el-button type="primary" @click="editCompany"
            >修改企业信息</el-button
          >
          <el-button type="primary" @click="toProject">查看企业项目</el-button>
          <el-button type="success" @click="submitCompany">企业提交审核</el-button>
        </div> -->
      </div>

      <div>
        <el-row class="rowBorder rowTopBorder">
          <el-col :span="4" class="col-flex bg-ccc">企业名称</el-col>
          <el-col :span="8" class="col-flex colLeftBorder leftAlign-pd30 colRightBorder">{{
            companyInfo.name
          }}</el-col>
          <el-col :span="4" class="col-flex bg-ccc">统一社会信用代码</el-col>
          <el-col :span="8" class="col-flex colLeftBorder leftAlign-pd30">{{
            companyInfo.code
          }}</el-col>
        </el-row>
        <el-row class="rowBorder">
          <el-col :span="4" class="col-flex bg-ccc">法定代表人</el-col>
          <el-col :span="8" class="col-flex colLeftBorder leftAlign-pd30 colRightBorder">{{
            companyInfo.legalPerson
          }}</el-col>
          <el-col :span="4" class="col-flex bg-ccc">注册资本(万元)</el-col>
          <el-col :span="8" class="col-flex colLeftBorder leftAlign-pd30">{{
            companyInfo.capital
          }}</el-col>
        </el-row>
        <el-row class="rowBorder">
          <el-col :span="4" class="col-flex bg-ccc">负责人</el-col>
          <el-col :span="8" class="col-flex colLeftBorder leftAlign-pd30 colRightBorder">{{
            companyInfo.linkman
          }}</el-col>
          <el-col :span="4" class="col-flex bg-ccc">负责人联系方式</el-col>
          <el-col :span="8" class="col-flex colLeftBorder leftAlign-pd30">{{
            companyInfo.phone
          }}</el-col>
        </el-row>
        <el-row class="rowBorder">
          <el-col :span="4" class="col-flex bg-ccc">注册地址</el-col>
          <el-col :span="20" class="col-flex colLeftBorder leftAlign-pd30">
            {{ companyInfo.registerAddress }}
          </el-col>
        </el-row>
        <el-row class="rowBorder">
          <el-col :span="4" class="col-flex bg-ccc">办公地址</el-col>
          <el-col :span="20" class="col-flex colLeftBorder leftAlign-pd30">{{
            companyInfo.officeAddress
          }}</el-col>
        </el-row>
        <el-row class="rowBorder">
          <el-col :span="4" class="col-flex bg-ccc">行业类别</el-col>
          <el-col :span="8" class="col-flex colLeftBorder leftAlign-pd30 colRightBorder">{{
            industryFormate(companyInfo.industry)
          }}</el-col>
          <el-col :span="4" class="col-flex bg-ccc">注册类型</el-col>
          <el-col :span="8" class="col-flex colLeftBorder leftAlign-pd30">{{
            registrationFormate(companyInfo.registration)
          }}</el-col>
        </el-row>
        <el-row class="rowBorder">
          <el-col :span="4" class="col-flex bg-ccc">纳税类型</el-col>
          <el-col :span="8" class="col-flex colLeftBorder leftAlign-pd30 colRightBorder">{{
            taxTypeFormate(companyInfo.taxType)
          }}</el-col>
          <el-col :span="4" class="col-flex bg-ccc">所属部门</el-col>
          <el-col :span="8" class="col-flex colLeftBorder leftAlign-pd30">{{
            officeIdFormate(companyInfo.officeId)
          }}</el-col>
        </el-row>
        <!-- <el-row class="rowBorder">
          <el-col :span="4" class="col-flex bg-ccc">企业状态</el-col>
          <el-col :span="8" class="col-flex colLeftBorder leftAlign-pd30 colRightBorder">{{
            companyStatusFormate(companyInfo.status)
          }}</el-col>
          <el-col :span="4" class="col-flex bg-ccc">驳回说明</el-col>
          <el-col :span="8" class="col-flex colLeftBorder leftAlign-pd30">{{
            companyInfo.memo
          }}</el-col>
        </el-row> -->
        <el-row class="rowBorder bg-ccc">
          <el-col :span="4" class="">
            <div>
              <div class="textCenter">营业执照</div>
              <div class="textCenter">(点击下载)</div>
            </div></el-col
          >
          <el-col :span="20" class="col-flex leftAlign-pd30 colLeftBorder leftAlign-pd30 bg-fff">
            <div style="margin:10px">
                <el-image
                  style="width: 100px; height: 100px"
                  :src="companyInfo.meta"
                  placeholder="请上传营业执照"
                  error="请上传营业执照"
                  fit="contain"
                >
                </el-image>
                <div class="flexWithJusCenter">
                   <el-link :href="companyInfo.meta" target="_blank" type="primary" class="link">营业执照</el-link> 
                </div>
              </div>
          </el-col>
        </el-row>
      </div>
      <div class="mg-T20">
        <el-form
          :model="auditForm"
          :rules="auditFormRules"
          ref="auditForm"
          label-width="80px"
          class="demo-ruleForm"
        >
          <el-form-item label="驳回说明" prop="memo" v-if="type == 'audit'">
            <el-input type="age" v-model="auditForm.memo"></el-input>
          </el-form-item>
          <el-form-item v-if="type == 'audit'">
            <el-button type="primary" @click="adopt('auditForm')"
              >审核通过</el-button
            >
            <el-button type="danger" @click="reject('auditForm')"
              >审核驳回</el-button
            >
          </el-form-item>
          <el-form-item v-if="type == 'edit'">
            <el-button type="primary" @click="adoptEdit('auditForm')"
              >同意订正</el-button
            >
            <el-button type="danger" @click="rejectEdit('auditForm')"
              >订正驳回</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { find as companyFind } from "@/api/company";
import {
  adopt as companyAdopt,
  reject as companyReject,
  update, 
} from "@/api/adminCompany";
import {list as officeList} from '@/api/sysOffice'
import { list as dictList } from "@/api/dict";
import utils from "@/utils/utils.js";

export default {
  components: {},
  name: "companyAudit",
  data() {
    var alidateMemo = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入驳回说明"));
      } else {
        callback();
      }
    };

    return {
      companyId: "",
      companyInfo: {
        code: "",
        name: "",
        legalPerson: "",
        capital: "",
        linkman: "",
        phone: "",
        registerAddress: "",
        officeAddress: "",
        industry: "",
        registration: "",
        taxType: "",
        status: "",
        memo: "",
        deleted: "",
        officeId: "",
        userId: "",
        meta: "",
        createBy: "",
        modifyBy: "",
      },

      //字典数据
      taxTypeDict: [],
      registrationDict: [],
      companyStatusDict: [],
      industryDict:[],

      //获取字典的promise
      taxTypeDictPromise: "",
      registrationDictPromise: "",
      companyStatusDictPromise:"",
      officeListPromise:"",
      industryDictPromise:"",

      //审核表单
      auditForm: {
        memo: "", //驳回说明
      },
      auditFormRules: {
        memo: [{ validator: alidateMemo, trigger: "blur" }],
      },

      //类型
      type:"",
      path: "",
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.path = route.path
      },
      immediate: true,
    },
  },

  activated() {
    console.log("activated执行缓存--", this.$store.getters);
    if (this.$store.getters.closeAliveName == "companyAudit") {
      console.log("activated执行要数据刷新了--", this.$store.getters);
      if(this.companyId != this.$route.query.companyId || this.type != this.$route.query.type){
        this.auditForm.memo = ""
        this.type = this.$route.query.type
        this.companyId = this.$route.query.companyId;
        this.companyFind(this.companyId);
      }
      this.$store.dispatch("user/cleanCloseAliveName");
    }
  },

  created() {
    console.log("created执行--");
    if (this.$route.query) {
      this.companyId = this.$route.query.companyId;
      this.type = this.$route.query.type
      console.log('this.type--',this.type);
    }
    this.taxTypeDictPromise = utils.getDictList("tax_type", (res) => {
      this.taxTypeDict = res.data;
    });
    this.registrationDictPromise = utils.getDictList("registration", (res) => {
      this.registrationDict = res.data;
    });
    this.companyStatusDictPromise = utils.getDictList(
      "company_status",
      (res) => {
        this.companyStatusDict = res.data;
      }
    );
    this.industryDictPromise = utils.getDictList("industry", (res) => {
      this.industryDict = res.data;
    });
    this.officeListPromise = this.officeList()
  },

  mounted() {
    Promise.all([
      this.taxTypeDictPromise,
      this.registrationDictPromise,
      this.companyStatusDictPromise,
      this.officeListPromise,
      this.industryDictPromise
    ]).then((res) => {
      //console.log("字典已经全部加载完成--");
      this.companyFind(this.companyId);
    });
  },

  destroyed() {},

  methods: {
    //获取公司信息
    companyFind(id) {
      companyFind(id).then((res) => {
        console.log("获取公司信息res--", res);
        if (res.type == "success") {
          this.companyInfo = res.data.company;
          // if (!utils.isNull(this.companyInfo.meta)) {
          //   this.companyInfo.meta = JSON.parse(this.companyInfo.meta);
          // }
        }
        console.log("this.companyInfo--", this.companyInfo);
      });
    },

    //企业审核
    adopt(Form) {
      let params = new URLSearchParams();
      params.append("companyId", this.companyInfo.id);
      companyAdopt(params).then((res) => {
        console.log("企业的审核通过--", res);
        if (res.type == "success") {
          this.$message.success("企业的审核通过");
          this.$store.dispatch("user/setCloseView", this.path)
          this.$router.push({
            path: "/company/auditList",
          });
        }
      });
    },

    //审核驳回
    reject(formName) {
      this.$refs[formName].validate((valid) => {
        console.log("valid--", valid);
        if (valid) {
          let params = new URLSearchParams();
          params.append("companyId", this.companyInfo.id);
          params.append("rejectText", this.auditForm.memo);
          companyReject(params).then((res) => {
            console.log("企业的驳回--", res);
            if (res.type == "success") {
              this.$message.success("企业的审核已经驳回");
              this.$store.dispatch("user/setCloseView", this.path)
              this.$router.push({
                path: "/company/auditList",
              });
            }
          });
        }
      });
    },

    adoptEdit(){
      this.companyInfo.status = 0
      update(this.companyInfo).then((res) => {
            console.log("统一订正--", res);
            if (res.type == "success") {
              this.$message.success("企业的订正申请已经同意");
              this.$store.dispatch("user/setCloseView", this.path)
              this.$router.push({
                path: "/company/auditList",
              });
            }
          });
    },

    rejectEdit(){
      this.companyInfo.status = 2
      update(this.companyInfo).then((res) => {
            console.log("企业的驳回--", res);
            if (res.type == "success") {
              this.$message.success("企业的订正申请已经驳回");
              this.$store.dispatch("user/setCloseView", this.path)
              this.$router.push({
                path: "/company/auditList",
              });
            }
          });
    },

    officeList() {
        let _this = this;
        let params = new URLSearchParams();
        params.append('menuType', 0);
        return officeList(params).then(response => {
          console.log(response)
          _this.offices = response.data;
        }).catch(error => {
          console.log('error!!')
        });
      },

    //字典数据格式化
    taxTypeFormate(data) {
      return utils.selectDictLabel(this.taxTypeDict, data);
    },

    registrationFormate(data) {
      return utils.selectDictLabel(this.registrationDict, data);
    },

    companyStatusFormate(data) {
      return utils.selectDictLabel(
        this.companyStatusDict,
        "company_status-" + data
      );
    },
    industryFormate(data) {
      return utils.selectDictLabel(this.industryDict, data);
    },

    officeIdFormate(data) {
      return utils.selectDictLabel(this.offices, data);
    },
  },
};
</script>

<style lang="scss" scoped>
</style>
